//
//  StorageBaseViewController.m
//  danpin
//
//  Created by chuangjia on 1/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "StorageBaseViewController.h"
#import "UserNavigationController.h"
@interface StorageBaseViewController ()<UserDelegate>

{
    
}
@property(nonatomic,strong) UIButton *backButton;
@property(nonatomic,strong) UILabel *titleLabel;
@property(nonatomic,strong) UIButton *rightButton;
@property(nonatomic,strong)  UIView *headerView;
@end

@implementation StorageBaseViewController
@synthesize headerView=_headerView;
@synthesize navTitle=_navTitle;
@synthesize isIndex=_isIndex;
@synthesize backButton=_backButton;
@synthesize rightButton=_rightButton;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationController.navigationBarHidden=YES;
    [self initNavView];
}
-(void)dealloc
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
    self.backButton=nil;
    self.rightButton =nil;
    self.titleLabel=nil;
    self.navTitle=nil;
    self.headerView = nil;
    //    NSLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationSlide];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)isLogin
{
    if ([[ZShare sharedInstance] isLogin]) {
        return YES;
    }else{
        
        UserNavigationController *viewcontroller =[UserNavigationController newWithCameraDelegate:self];
        [self presentViewController:viewcontroller animated:NO completion:nil];
        
        return NO;
    }
    return NO;
}
-(void)userLoginOK:(id)userinfo
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)userDidCancel
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)setNavTitle:(NSString *)navTitle
{
    _navTitle=navTitle;
    _titleLabel.text = navTitle;
}


-(void)setRightTitle:(NSString *)rightTitle
{
    _rightTitle=rightTitle;
    _backButton.hidden =NO;
    _rightButton.hidden = NO;
    [_rightButton setTitle:rightTitle forState:UIControlStateNormal];
}
-(CGFloat)navHeight
{
    return 65;
}
-(void)setIsIndex:(BOOL)isIndex
{
    if (isIndex) {
        _backButton.hidden =YES;
        _rightButton.hidden = NO;
    }else{
        _backButton.hidden =NO;
        _rightButton.hidden = YES;
    }
}
-(void)initNavView
{
    _headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 65)];
    _headerView.backgroundColor=[UIColor clearColor];
    UIImageView *backView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,65)];
    backView.image =[UIImage imageNamed:@"titile_bg.png"];
    [_headerView addSubview:backView];
    backView=nil;
    
    _titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 20+(65-20-20)/2, self.view.frame.size.width, 20)];
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = UIColorFromRGB(0x808080);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = FONT(16);
    [_headerView addSubview:_titleLabel];
    //
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _backButton.frame = CGRectMake(0, 20+(65-20-25)/2, 30, 25);
    _backButton.tag =0;
    [_backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:_backButton];
    //
    //
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(DEVICE_WIDTH -24- PHOTO_FRAME_WIDTH*2,18+(hightAuto(65)-24-20)/2, 24, 24);
    _rightButton.tag =1;
    _rightButton.titleLabel.font =FONT(14);
    [_rightButton setImage:[UIImage imageNamed:@"title_search_ico.png"] forState:UIControlStateNormal];
    [_rightButton setTitleColor:UIColorFromRGB(0x285790) forState:UIControlStateNormal];
    [_rightButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:_rightButton];
    [self.view addSubview:_headerView];
    

}

-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton *)sender;
    switch (btn.tag) {
        case 0:
        {
            [NSObject cancelPreviousPerformRequestsWithTarget:self];
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
            break;
        default:
            break;
    }
}
-(UIView *)footerView:(NSString*)msg
{
    if (msg==nil || msg.length==0) {
        msg=@"单品 - 以一顶百";
    }
    UIView *footerview =[[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 90)];
    footerview.backgroundColor=[UIColor clearColor];
    UILabel *footLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 20)];
    footLabel.backgroundColor = [UIColor clearColor];
    footLabel.textColor = UIColorFromRGB(0xcccccc);
    footLabel.textAlignment = NSTextAlignmentCenter;
    footLabel.font = FONT(12);
    footLabel.text =msg;
    [footerview addSubview:footLabel];
    return footerview;
}
-(void)hiddenNavView
{
    _headerView.alpha = 0.0;
    _headerView.hidden = YES;
}
-(void)showNavView
{
    _headerView.alpha = 1.0;
    _headerView.hidden = NO;
}
@end